package com.test.burnincash.bean_burnin.config_info_burnin;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.text.TextUtils;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.test.burnincash.util_burnin.StringUtil_burnin;
import com.test.burnincash.util_burnin.ThreadUtil_burnin;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;

public class AppShowInfo_burnin {

    private static class Inner {
        private static AppShowInfo_burnin instance = new AppShowInfo_burnin();
    }

    private AppShowInfo_burnin() {
    }

    public static AppShowInfo_burnin getInstance() {
        return Inner.instance;
    }

    private Customize customize;
    private ArrayList<WalletChannel> walletChannels;

    public static void parse(JSONObject objJson) {
        //if (objJson==null) return;
        Gson gson = new Gson();
        Inner.instance = gson.fromJson(objJson.toString(), AppShowInfo_burnin.class);
        if (Inner.instance.customize == null)
            Inner.instance.customize = gson.fromJson(objJson.optString("customize"), Customize.class);
    }

    /**
     * 配置的AF key
     */
    public String getAFKey() {
        if (customize == null) return "";
        return customize.getAfKey();
    }

    public String getMobile() {
        String[] mobiles = getMobiles();
        if (mobiles.length > 0) return mobiles[0];
        return "";
    }

    public String getEmail() {
        String[] email = getEmails();
        if (email.length > 0) return email[0];
        return "";
    }

    public String[] getMobiles() {
        if (customize == null) return new String[0];
        return customize.getMobile();
    }

    public String[] getEmails() {
        if (customize == null) return new String[0];
        return customize.getEmail();
    }

    public String[] getPermissions() {
        ArrayList<String> permission = customize.getPermissions().getPermission();
        if (permission == null) return new String[0];
        String[] result = new String[permission.size()];
        for (int i = 0; i < permission.size(); i++) {
            result[i] = permission.get(i);
        }
        return result;
    }

    public String[] getHome() {
        ArrayList<String> permission = customize.getPermissions().getHome();
        if (permission == null) return new String[0];
        String[] result = new String[permission.size()];
        for (int i = 0; i < permission.size(); i++) {
            result[i] = permission.get(i);
        }
        return result;
    }

    public String getTransferDelayImportant() {
        if (customize == null) return "";
        return StringUtil_burnin.getSafeString(customize.getTransferDelayImportant());
    }

    public String getRejectPeriod() {
        if (customize == null) return "";
        return customize.getRejectPeriod();
    }

    public String isTestAccount(String mobile) {
        if (customize.accList == null || customize.accList.size() < 1) return "";
        String str = customize.accList.get(0).toString();
        try {
            JSONObject jsonObject = new JSONObject(str);
            if (jsonObject.optString("accM").equalsIgnoreCase(mobile))
                return jsonObject.optString("accTk");
        } catch (JSONException e) {
            throw new RuntimeException(e);
        }
        return "";
    }

    /**
     * 是否需要申请UsageStats权限
     */
    public boolean needRequestUsageStatsPermission() {
        if (TextUtils.isEmpty(customize.usageStatsPermission)) {
            return false;
        } else {
            return customize.usageStatsPermission.equalsIgnoreCase("YES");
        }
    }

    public int getUsageStatsDays() {
        return customize.usageStatsDays;
    }

    public static class Customize {

        private String homeAmount;
        private String noPointRecord;
        private String companyAddress;
        private String mobile;
        private String company;
        private String afKey;
        private String email;
        private String repayType;
        private String rejectPeriod;
        private JsonArray walletChannels;
        private String transferDelayImportant;
        private JsonArray accList;
        private AppShowPermissions_burnin permissions;
        /**
         * if YES, request PACKAGE_USAGE_STATS permission, or do nothing
         */
        private String usageStatsPermission;
        /**
         * usage stats days
         */
        private int usageStatsDays = 7;

        private Customize() {
        }

        public String getHomeAmount() {
            return StringUtil_burnin.getSafeString(homeAmount);
        }

        public String getNoPointRecord() {
            return StringUtil_burnin.getSafeString(noPointRecord);
        }

        public String getCompanyAddress() {
            return StringUtil_burnin.getSafeString(companyAddress);
        }

        public String[] getMobile() {
            return StringUtil_burnin.getSafeString(mobile).split(",");
        }

        public String getCompany() {
            return StringUtil_burnin.getSafeString(company);
        }

        public String getAfKey() {
            return StringUtil_burnin.getSafeString(afKey);
        }

        public String[] getEmail() {
            return StringUtil_burnin.getSafeString(email).split(",");
        }

        public String getRepayType() {
            return StringUtil_burnin.getSafeString(repayType);
        }

        public String getRejectPeriod() {
            return StringUtil_burnin.getSafeString(rejectPeriod);
        }

        public JsonArray getWalletChannels() {
            return walletChannels;
        }

        public AppShowPermissions_burnin getPermissions() {
            if (permissions==null) return new AppShowPermissions_burnin();
            return permissions;
        }

        public String getTransferDelayImportant() {
            return StringUtil_burnin.getSafeString(transferDelayImportant);
        }
    }

    private void initWalletChannels() {
        walletChannels = new ArrayList<>();
        JsonArray jsonArray = customize.getWalletChannels();
        Gson gson = new Gson();
        if (jsonArray != null) {
            for (int i = 0; i < jsonArray.size(); i++) {
                WalletChannel walletChannel = gson.fromJson(jsonArray.get(i).toString(), WalletChannel.class);
                walletChannels.add(walletChannel);
            }
        }

        //获取图标
        ThreadUtil_burnin.getInstance().runOnChildThread(() -> {
            for (WalletChannel walletChannel : walletChannels) {
                try {
                    if (!TextUtils.isEmpty(walletChannel.getLogo())) {
                        URL url = new URL(walletChannel.getLogo());
                        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                        connection.setConnectTimeout(5000);
                        connection.setRequestMethod("GET");
                        if (connection.getResponseCode() == 200) {
                            InputStream inputStream = connection.getInputStream();
                            Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
                            walletChannel.setBitmap(bitmap);
                        }
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public ArrayList<WalletChannel> getWalletChannels() {
        if (walletChannels == null) {
            initWalletChannels();
        }
        return walletChannels;
    }

    public static class WalletChannel {

        private String desc;
        private String value;
        private String logo;

        private Bitmap bitmap;

        public String getDesc() {
            return StringUtil_burnin.getSafeString(desc);
        }

        public String getValue() {
            return StringUtil_burnin.getSafeString(value);
        }

        public String getLogo() {
            return StringUtil_burnin.getSafeString(logo);
        }

        public Bitmap getBitmap() {
            return bitmap;
        }

        public void setBitmap(Bitmap bitmap) {
            this.bitmap = bitmap;
        }

    }

}
